package com.youbff.task.core.base;

import org.apache.log4j.Logger;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.JobParametersInvalidException;
import org.springframework.batch.core.configuration.JobLocator;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
import org.springframework.batch.core.repository.JobRestartException;
import org.springframework.scheduling.quartz.QuartzJobBean;

/**
 * 定时任务job
 * @author lixuefeng
 * @email youbff@163.com
 * @time 2018年1月25日 下午5:23:18
 */
public class JobQuartz extends QuartzJobBean {
    private static final Logger logger = Logger.getLogger(JobQuartz.class);
    /** 实际执行任务. */
    private Job task;
    /** 任务存储器. */
    private JobLocator jobLocator;
    /** 作业调度器. */
    private JobLauncher jobLauncher;
    
    @Override
    protected void executeInternal(JobExecutionContext context)
            throws JobExecutionException {
        JobParameters jobParameters= new JobParametersBuilder().addLong("time", System.currentTimeMillis())  
                .toJobParameters(); 
        try {
            jobLauncher.run(task, jobParameters);
        } catch (JobExecutionAlreadyRunningException | JobRestartException
                | JobInstanceAlreadyCompleteException
                | JobParametersInvalidException e) {
            e.printStackTrace();
            logger.error("执行作业"+task.getName()+"异常", e);
        }  
    }

    public Job getTask() {
        return task;
    }

    public void setTask(Job task) {
        this.task = task;
    }

    public JobLocator getJobLocator() {
        return jobLocator;
    }

    public void setJobLocator(JobLocator jobLocator) {
        this.jobLocator = jobLocator;
    }

    public JobLauncher getJobLauncher() {
        return jobLauncher;
    }

    public void setJobLauncher(JobLauncher jobLauncher) {
        this.jobLauncher = jobLauncher;
    }
    
    

}
